import axios from 'axios';
// import * as types from '@/store/mutation-types'

var _axios = axios.create({
    baseURL: process.env.VUE_APP_BASE_API
})

_axios.interceptors.request.use(
    (config) => {
        // if (VM.$store.state.appState.userInfo) {
        //     config.headers.token = VM.$store.state.appState.userInfo.token;
        // } 
        return config;
    }, (error) => {
        return Promise.reject(error);
    }
);

_axios.interceptors.response.use(
    (response) => {
        // 登录失败
        // if(response.data.retCode === "no_token"){
        //     VM.$router.push("login")
        // }
        // VM.$store.commit(types.SET_appState, {loginInfo:{token:token}})
        return response;
    }, (error) => {
        // window.VM.$store.commit(types.LOADING_COUNT_MINUS)
        let message = '请求错误'
        if (error.response) {
            switch (error.response.status) {
                case 400:
                message = '请求错误'
                break
                case 401:
                    message = '未授权，请登录'
                    // localStorage.clear();
                    break
                case 403:
                  message = '拒绝访问'
                    break
                case 404:
                  message = `请求地址出错`
                    break
                case 408:
                    message = '请求超时'
                   break
                case 500:
                    message = '服务器内部错误'
                    break
                case 501:
                    message = '服务未实现'
                    break
                case 502:
                    message = '网关错误'
                    break
                case 503:
                    message = '服务不可用'
                    break
                case 504:
                    message = '网关超时'
                    break
                case 505:
                    message = 'HTTP版本不受支持'
                    break
            }
            // `请求地址出错: ${error.response.config.url}`
            // VM.$notification.error({
            //     message: VM.$t('错误'),
            //     description: VM.$t(message)
            // });
        }
        return Promise.reject(new Error(res.message || 'Error'))
    }
);

export default _axios